package com.example.demo.util;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.*;
import java.net.URI;

/**
 * 带参数的GET请求
 * 两种方式：
 *       1.直接将参数拼接到url后面 如：?wd=java
 *       2.使用URI的方法设置参数 setParameter("wd", "java")
 */
public class UrlCheckHandler {
    public static void main(String[] args) throws Exception {

        File file = new File("D:\\output\\success_20230927_2.txt");

        FileWriter fw = null;
        BufferedWriter bw = null;


        File livefile = new File("D:\\output\\success_202309271653_2.txt");

        FileWriter livefw = null;
        BufferedWriter livebw = null;

        File nofile = new File("D:\\output\\nofile_2.txt");
        FileWriter nofw = null;
        BufferedWriter nobw = null;
        // 定义请求的参数
//        URI uri = new URIBuilder("http://www.baidu.com/s").setParameter("wd", "java").build();

        CloseableHttpResponse response = null;
        CloseableHttpClient httpclient = null;
        try {
            FileInputStream inputStream = new FileInputStream("D:\\input\\unsuccess_input_2.txt");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

            if(!file.exists()) {
                file.createNewFile();
            }

            fw = new FileWriter( file.getAbsoluteFile(), true);  //true续写新内容
            bw = new BufferedWriter(fw);

            if(!livefile.exists()) {
                livefile.createNewFile();
            }

            livefw = new FileWriter( livefile.getAbsoluteFile(), true);  //true续写新内容
            livebw = new BufferedWriter(livefw);

            if(!nofile.exists()) {
                nofile.createNewFile();
            }

            nofw = new FileWriter( nofile.getAbsoluteFile(), true);  //true续写新内容
            nobw = new BufferedWriter(nofw);

            String str = null;
            int i = 0;
            while ((str = bufferedReader.readLine()) != null) {
                // 创建Httpclient对象
                i++;
                System.out.println("正在处理完成："+str+"   个数："+i);
                String subStr = str.substring(0,str.indexOf(".mp4"));
                for(int n = 2;n<6;n++){
                    httpclient = HttpClientBuilder.create().build();
                    String extStr = "_"+n+".mp4";
                    String urlStr = subStr + extStr;
                    URI uri = new URIBuilder(urlStr).build();

                    // 创建http GET请求
                    HttpGet httpGet = new HttpGet(uri);
                    //response 对象
                    // 执行http get请求
                    response = httpclient.execute(httpGet);
                    // 判断返回状态是否为404
                    if (response.getStatusLine().getStatusCode() == 200) {
                        String updateSql = "";
                        if(urlStr.indexOf("20230927/")>0){
                            updateSql = "update medic_live_replay_manage_record set replay_url_record='"+urlStr+"' where replay_url_record='"+str+"';";
                            bw.write(updateSql+"\r\n");
                            bw.flush();
                        }else{
                            updateSql = "update live_replay_record set replay_url_record='"+urlStr+"' where replay_url_record='"+str+"';";
                            livebw.write(updateSql+"\r\n");
                            livebw.flush();
                        }

                        break;
                    }else{
                        if(n==5){
                            nobw.write(str+"\r\n");
                            nobw.flush();
                        }else {
                            continue;
                        }
                    }
                }
                System.out.println("处理完成："+str+"   个数："+i);

            }

            //close
            bw.close();
            livebw.close();
            nobw.close();
            inputStream.close();
            bufferedReader.close();

        } finally {
            if (response != null) {
                response.close();
            }
            if(httpclient != null){
                httpclient.close();
            }
        }
    }
}



